import Taro from "@tarojs/taro";
import { requestPayment } from "@tarojs/taro";
const baseUrl =
  process.env.TARO_APP_BASE_URL || "https://car-admin.xipeisoft.com";

export async function initiateWeChatPay(orderData) {
  try {
    // 调用后台接口获取 prepay_id
    const result = await Taro.request({
      url: baseUrl + "/api/wechat-pay/order",
      method: "POST",
      data: orderData,
    });
    const data = result.data.data;

    // 调起微信支付
    const paymentResponse = await requestPayment(data);
    console.log("Payment success:", paymentResponse);

    if (paymentResponse.errMsg === "requestPayment:ok") {
      // 支付成功
      return true;
    } else {
      return false;
    }
  } catch (error) {
    console.error("Payment failed:", error);
    return false;
  }
}
